package com.bourne.exception;

import com.bourne.enums.BizCodeEnum;
import com.bourne.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ValidationException;

/**
 * 异常处理类
 * @PROJECT_NAME:
 * @DATE: 2021/3/22 20:19
 */
@ControllerAdvice
@Slf4j
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData Handle(Exception e) {
        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            log.info("[业务异常]{}", String.valueOf(e));
            return JsonData.buildError(bizException.getCode(), bizException.getMsg());

        } else if (e instanceof MethodArgumentNotValidException) {
            log.info("[业务异常]{}", String.valueOf(e));
            MethodArgumentNotValidException notValidException = (MethodArgumentNotValidException) e;
            return JsonData.buildError(BizCodeEnum.METHOD_ARGUMENT_NOT_VALID.getCode(),
                    "方法或参数校验失败:" + notValidException.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            // return JsonData.buildResult(BizCodeEnum.METHOD_ARGUMENT_NOT_VALID);
        } else if (e instanceof ValidationException) {
            log.info("[业务异常]{}", String.valueOf(e));
            return JsonData.buildResult(BizCodeEnum.REQUEST_ARGS_NOT_VALID);
        }  else if (e instanceof NoHandlerFoundException) {
            log.info("[业务异常]{}", String.valueOf(e));
            return JsonData.buildResult(BizCodeEnum.REQUEST_ROUTE_NOT_FOUND);
        } else if (e instanceof DuplicateKeyException) {
            log.info("[业务异常]{}", String.valueOf(e));
            return JsonData.buildResult(BizCodeEnum.DATE_REPEAT_COMMIT);
        }
//        else if (e instanceof MQClientException || e instanceof MQBrokerException) {
//            return JsonData.buildResult(BizCodeEnum.MQ_CONFIG_FAIL);
//        }
        else if (e instanceof BindException) {
            return JsonData.buildResult(BizCodeEnum.METHOD_ARGUMENT_NOT_VALID);
        } else if(e instanceof HttpRequestMethodNotSupportedException) {
            return JsonData.buildResult(BizCodeEnum.METHOD_NOT_SUPPORTED);
        } else if (e instanceof RedisConnectionFailureException) {
            return JsonData.buildResult(BizCodeEnum.REDIS_CONNECTION_FAIL);
        }
        else {
            log.info("[业务异常]{}", String.valueOf(e));
            return JsonData.buildResult(BizCodeEnum.SYSTEM_ERROR);
        }
    }
}
